﻿<!--
    #region License

// Copyright (c) 2012, Zhiyong Yang.
// All rights reserved.
// 
// 
// This software is licensed under the GNU General Public License version 2 (GPLv2).
// 
// Contact Me: 
//  Mail: younthu@yahoo.com.cn
//  MSN:  evil_eve_live@hotmail.com

#endregion
    -->
    
    <UserControl x:Class="Bubbels.Controls.Board"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:Helper="clr-namespace:Bubbels.Helper"
             xmlns:local="clr-namespace:Bubbels"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <Helper:PositionConverter  x:Key="positionConverter">
        </Helper:PositionConverter>
        
        <Helper:BallColorConverter x:Key="colorConverter"></Helper:BallColorConverter>

        <Storyboard x:Key="FadeOutStoryboard" Completed="Storyboard_Completed">
            <DoubleAnimation Storyboard.TargetProperty="(Label.Width)" To="0" Duration="0:0:0.3" FillBehavior="HoldEnd">


            </DoubleAnimation>
            <DoubleAnimation Storyboard.TargetProperty="(Label.Height)" To="0" Duration="0:0:0.3" FillBehavior="HoldEnd"  />

        </Storyboard>
        <Storyboard x:Key="CanvasLeftStory">
            <DoubleAnimation Storyboard.TargetProperty="Canvas.Left" To="{Binding Path=Column, Converter={StaticResource positionConverter}, ConverterParameter=False}" />

        </Storyboard>

        <Storyboard x:Key="CanvasTopStory">
            <DoubleAnimation Storyboard.TargetProperty="Canvas.Top" To="{Binding Path=Row, Converter={StaticResource positionConverter}, ConverterParameter=True}" />

        </Storyboard>
    </UserControl.Resources>
    <ItemsControl ItemsSource="{Binding Path=Balls}" Background="#99FF00FF">
        <ItemsControl.ItemTemplate>
            <DataTemplate >
                <Label Width="{Binding Source={StaticResource positionConverter}, Path=BallSize}" Height="{Binding Source={StaticResource positionConverter}, Path=BallSize}" Margin="0" Padding="0" MouseEnter="Label_MouseEnter" MouseUp="Label_MouseUp">

                    <Ellipse Stretch="UniformToFill">
                        <Ellipse.Fill>
                            <RadialGradientBrush GradientOrigin="0.75,0.25">
                                <GradientStop Color="White" Offset="0.0"></GradientStop>
                                <GradientStop  Offset="0.5" Color="{Binding Path=BallType, Converter={StaticResource colorConverter}}" />
                                <GradientStop Color="Black" Offset="1.0"></GradientStop>
                            </RadialGradientBrush >
                        </Ellipse.Fill>
                    </Ellipse>

                    <Label.Style>
                        <Style  TargetType="Label">
                            <Setter Property="Background" Value="Transparent"></Setter>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Selected}" Value="True">
                                    <Setter  Property="Background" Value="Red" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Removed}" Value="True" >
                                    <!--<EventSetter Event="DoubleAnimation.Completed" Handler="Storyboard_Completed"></EventSetter>-->

                                    <DataTrigger.EnterActions >

                                        <BeginStoryboard Name="RemovedStoryboard" Storyboard="{StaticResource FadeOutStoryboard}">
                                        </BeginStoryboard>
                                        
                                    </DataTrigger.EnterActions>
                                    <!-- Explosion here -->
                                    <DataTrigger.ExitActions >
                                        
                                    </DataTrigger.ExitActions>
                                </DataTrigger>

                                <EventTrigger RoutedEvent="Label.Loaded" >
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <!--{Binding Source={StaticResource positionConverter}, Path=BallSize}-->
                                            <!--<DoubleAnimation Storyboard.TargetProperty="(Label.Width)" From="0" To="70" Duration="0:0:0.3" FillBehavior="Stop" />
                                            <DoubleAnimation Storyboard.TargetProperty="(Label.Height)" From="0" To="70" Duration="0:0:0.3" FillBehavior="Stop"/>-->
                                        </Storyboard>
                                    </BeginStoryboard>
                                    <EventTrigger.ExitActions>
                                       
                                    </EventTrigger.ExitActions>
                                </EventTrigger>
                            </Style.Triggers>
                        </Style>
                    </Label.Style>
                </Label>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas>
                </Canvas>

            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                
                <Setter Property="Canvas.Left" Value="{Binding Path=Column, Converter={StaticResource positionConverter}, ConverterParameter=False}" />
                <Setter Property="Canvas.Top" Value="{Binding Path=Row, Converter={StaticResource positionConverter}, ConverterParameter=True}" />
            </Style>
        </ItemsControl.ItemContainerStyle>

    </ItemsControl>
</UserControl>
